package com.gree.scmservice.dao.saasDao;

import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasCommodity;
import com.gree.scmservice.entity.saas.SaasSysOrder;
import org.nutz.dao.*;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.lang.util.NutMap;
import org.nutz.trans.Atom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 包路径: com.gree.scmservice.dao.saasDao
 * 项目名: scm-api
 * 创建时间: 2021-07-14 14:29
 * 描述: TODO
 **/
@Repository
public class SaasOrderDao {

    @Autowired
    private Dao dao;

    /**
     * 新增
     * @param saasSysOrder
     * @return
     */
    public SaasSysOrder insertSaasOrder(SaasSysOrder saasSysOrder){
        SaasSysOrder insert = dao.insert(saasSysOrder);
        return insert;
    }

    /**
     * 更新
     * @param saasSysOrder
     * @return
     */
    public int updateSaasOrder(SaasSysOrder saasSysOrder){
        final int[] a = {0};
        FieldFilter.locked(SaasCommodity.class,"^create_time$").run(new Atom() {
            @Override
            public void run() {
                a[0] = dao.update(saasSysOrder);
            }
        });
        return a[0];
    }


    /**
     * 关闭订单 1 删除
     * @param orderNo
     * @returnD
     */
    public int updateDelOrderById(String orderNo){
        int updateCount = dao.update(SaasSysOrder.class,Chain.make("is_delete",1),
                Cnd.where("order_no","=",orderNo));
        return updateCount;
    }


    /**
     * 关闭订单 1 删除
     * @returnD
     */
    public int updateDelOrderByIds(String[] orderNos){
        int updateCount = dao.update(SaasSysOrder.class,Chain.make("is_delete",2),
                Cnd.where("order_no","in",orderNos));
        return updateCount;
    }

    /**
     * 1.4、更新订单（审批通过）
     * @param orderNo
     * @returnD
     */
    public int updateStatusOrder(String orderNo,int orderStatus){
        int updateCount = dao.update(SaasSysOrder.class,Chain.make("ORDER_STATUS",orderStatus),
                Cnd.where("order_no","=",orderNo));
        return updateCount;
    }

    /**
     * 线下支付
     * @param saasSysOrder
     * @return
     */
    public int updatePayTypeOrder(SaasSysOrder saasSysOrder){
        final int[] a = {0};
        FieldFilter.create(SaasSysOrder.class,"^order_no|payCert|payCertex|orderStatus|payTime|payType$", true).run(new Atom(){
            @Override
            public void run(){
                a[0] = dao.update(saasSysOrder);
            }
        });
        return a[0];
    }

    /**
     * 更新图片
     */
    public int updateOrderPayCert(String orderNo, String payCert){
        return dao.update(SaasCommodity.class, Chain.make("pay_cert", payCert), Cnd.where("order_no","=",orderNo));
    }

    /**
     * id查询
     * @param orderNo
     * @return
     */
    public SaasSysOrder getOrderNoIdOrder(String orderNo){
        SaasSysOrder saasSysOrder = dao.fetch(SaasSysOrder.class, orderNo);
        return saasSysOrder;
    }
    /**
     * 根据流水号查询订单详情
     * @param outTradeNo
     * @return
     */
    public SaasSysOrder getOutTradeNoOrder(String outTradeNo){
        SaasSysOrder saasSysOrder = dao.fetch(SaasSysOrder.class, Cnd.where("OUT_TRADE_NO", "=", outTradeNo));
        return saasSysOrder;
    }

    /**
     * 分页查询全部
     * @return
     */
    public TablePageData<SaasSysOrder> selectOrderPage(SaasSysOrder saasSysOrder){
        Pager pager = dao.createPager(saasSysOrder.getPageNum(), saasSysOrder.getPageSize());
        saasSysOrder.saasSysOrderNoPage();
        Cnd cnd = Cnd.from(dao,saasSysOrder);
        List query = dao.query(SaasSysOrder.class, cnd, pager);
        pager.setRecordCount(dao.count(SaasSysOrder.class, cnd));
        TablePageData<SaasSysOrder> tablePageData = new TablePageData(query, pager);
        return tablePageData;
    }

    /**
     * 检查当前商品是否存在待付款单据
     * @param clientId
     * @param isDelete
     * @param orderStatus
     * @param groupId
     * @return
     */
    public List<SaasSysOrder> getCheckOutTradeNo(String clientId,int isDelete,int orderStatus,String groupId){
        List<SaasSysOrder> query = dao.query(SaasSysOrder.class,
                Cnd.where("tenant_id", "=", clientId)
                        .and("is_delete", "=", isDelete)
                        .and("order_status", "=", orderStatus)
                        .and("group_id", "=", groupId));
       return query;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int delOrderNos(String[] ids) {
        int clear = dao.clear(SaasSysOrder.class, Cnd.where("order_no", "in", ids));
        return clear;
    }
}
